package mn.idax.exchange.otc.util;

import java.io.IOException;
import java.io.InputStream;

public class PicValidateUtil {

    public static final String TYPE_JPG = ".jpg";
    public static final String TYPE_GIF = ".gif";
    public static final String TYPE_PNG = ".png";
    public static final String TYPE_BMP = ".bmp";
    public static final String TYPE_UNKNOWN = "unknown";
    public static final String TYPE_JPG_HEX = "FFD8FF";
    public static final String TYPE_GIF_HEX = "47494638";
    public static final String TYPE_PNG_HEX = "89504E47";
    public static final String TYPE_BMP_HEX = "424D";

    public PicValidateUtil() {
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src != null && src.length > 0) {
            for(int i = 0; i < src.length; ++i) {
                int v = src[i] & 255;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    stringBuilder.append(0);
                }

                stringBuilder.append(hv);
            }

            return stringBuilder.toString();
        } else {
            return null;
        }
    }

    public static String getPicType(InputStream fis) {
        byte[] b = new byte[4];

        String var3;
        try {
            fis.read(b, 0, b.length);
            String type = bytesToHexString(b).toUpperCase();
            if (type.contains("FFD8FF")) {
                var3 = ".jpg";
                return var3;
            }

            if (!type.contains("89504E47")) {
                if (type.contains("47494638")) {
                    var3 = ".gif";
                    return var3;
                }

                if (type.contains("424D")) {
                    var3 = ".bmp";
                    return var3;
                }

                var3 = "unknown";
                return var3;
            }

            var3 = ".png";
        } catch (IOException var17) {
            var17.printStackTrace();
            return null;
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException var16) {
                    var16.printStackTrace();
                }
            }

        }

        return var3;
    }
}
